package com.yzj.android.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {


    private static DruidDataSource dataSource;
    static {
        Properties properies =new Properties();
        //读取 配置文件
        InputStream inputStream= JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            //从流加载数据
            properies.load(inputStream);

            dataSource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properies);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //获取连接  如果返回null  说明 连接失败
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection=dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return connection;
    }


    public static void close(Connection connection){

        //关闭数据库连接 返回数据连接池
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }




    }


}
